package com.cmwa.solution.business.investTrade.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.investTrade.entity.TradeOrderVo;
import com.cmwa.solution.business.investTrade.service.OrderManageService;
import com.cmwa.solution.utils.SessionUtils;

import net.sf.json.JSONObject;

/**
 * 交易订单管理
 * @author ex-chenbq
 *
 */
@Controller
@RequestMapping("/business/orderManage")
public class OrderManageController extends BaseController {

	private final static String JSP_PATH = "business/investTrade/";
	@Resource
	private OrderManageService orderManageService;
		
	/**
	 * 跳转交易订单页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/orderManageListView")
	public String orderManageListView(ModelMap model) {
		return JSP_PATH + "orderManageList";
	}
	
	/**
	 * 跳转交易下单页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeOrderAddView")
	public String tradeOrderAddView(ModelMap model) {
		return JSP_PATH + "tradeOrderAdd";
	}
	
	/**
	 * 交易订单数据分页查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/orderManageList")
	@ResponseBody
	public Page<TradeOrderVo> orderManageList(SearchParam param) {
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			param.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		Page<TradeOrderVo> list = orderManageService.queryTradeOrderListPage(param);
		return list;
	}
	
	/**
	 * 交易下单
	 * @param tradeOrderVo
	 * @return
	 */
	@RequestMapping("/insertTradeOrder")
	@ResponseBody
	public String insertTradeOrder(TradeOrderVo tradeOrderVo) {
		JSONObject jsonObject = new JSONObject();
		try {
			Employee employee = SessionUtils.getEmployee();
			tradeOrderVo.setCreateId(employee.getID());
			tradeOrderVo.setModifyId(employee.getID());
			tradeOrderVo.setStatus("C");
			orderManageService.insertTradeOrder(tradeOrderVo);
			jsonObject.put("resultCode", "0000");
			jsonObject.put("resultMsg", "交易下单成功！");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("resultCode", "9999");
			jsonObject.put("resultMsg", "交易下单失败："+e.getMessage());
		}
		return jsonObject.toString();
	}
}

